"""Move public engine conf like connector to features_params

Revision ID: 8e6d5acc7be1
Revises: 8b69979ffc31
Create Date: 2022-04-13 23:41:02.798665

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session

# revision identifiers, used by Alembic.
revision = '8e6d5acc7be1'
down_revision = '8b69979ffc31'
branch_labels = None
depends_on = None

Base = declarative_base()

class QueryEngine(Base):
    __tablename__ = "query_engine"

    id = sa.Column(sa.Integer, primary_key=True)
    status_checker = sa.Column(sa.String(255))
    feature_params = sa.Column(sa.JSON, default={}, nullable=False)

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('query_engine', sa.Column('feature_params', sa.JSON(), nullable=False))

    session = Session(bind=op.get_bind())

    engines = session.query(QueryEngine).all()
    for engine in engines:
        if not engine.status_checker:
            continue
        engine.feature_params = {
            'status_checker': engine.status_checker
        }
    session.commit()

    op.drop_column('query_engine', 'status_checker')
    op.drop_column('query_engine', 'control_params')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('query_engine', sa.Column('control_params', mysql.JSON(), nullable=False))
    op.add_column('query_engine', sa.Column('status_checker', mysql.VARCHAR(length=255), nullable=True))

    session = Session(bind=op.get_bind())
    engines = session.query(QueryEngine).all()
    for engine in engines:
        if engine.feature_params and engine.feature_params.get('status_checker', None):
            engine.status_checker = engine.feature_params['status_checker']
    session.commit()

    op.drop_column('query_engine', 'feature_params')
    # ### end Alembic commands ###
